//
//  KDWeiboPhotosView.m
//  PublicSentimentMonitorSystem
//
//  Created by 魏强 on 15/11/26.
//  Copyright © 2015年 魏强. All rights reserved.
//

#import "KDWeiboPhotosView.h"
#import "KDWeiboPhotoView.h"
#import "KDWebImageSize.h"
#import "KDPhoto.h"
#import "MJPhotoBrowser.h"
#import "MJPhoto.h"
#define KDWeiboPhotoWH 70
#define KDWeiboPhotoMargin 10
#define KDWeiboPhotoMaxCol(count) ((count==4)?2:3)
#define KDWeiboCellMaxPhotoCount 9
@interface KDWeiboPhotosView ()
@property (nonatomic, assign) CGSize imageSize;

@end
@implementation KDWeiboPhotosView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
         [self setUpPhoto];
    }
    return self;
}

/***  添加子控件 */
- (void)setUpPhoto
{
    for (int i = 0; i < KDWeiboCellMaxPhotoCount; i++) {
        KDWeiboPhotoView *photo = [[KDWeiboPhotoView alloc]init];
        photo.tag = i;
        photo.backgroundColor = [UIColor greenColor];
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]init];
        [tap addTarget:self action:@selector(photoDidClicked:)];
        [photo addGestureRecognizer:tap];
        [self addSubview:photo];
    }
}
/***  图片触发事件 */
#pragma mark - photoDidClicked
- (void)photoDidClicked:(UITapGestureRecognizer *)tap
{
    MJPhotoBrowser *browser = [[MJPhotoBrowser alloc]init];
    NSMutableArray *tempArr = [NSMutableArray array];
    for (int i = 0; i < self.photos.count; i++) {
        KDPhoto *photo = self.photos[i];
        MJPhoto *mjPhoto = [[MJPhoto alloc]init];
        mjPhoto.url = [NSURL URLWithString:photo.bmiddle_pic] ;
        mjPhoto.srcImageView = self.subviews[i];
        [tempArr addObject:mjPhoto];
    }
    browser.photos = tempArr;
    browser.currentPhotoIndex = tap.view.tag;
    [browser show];
}

- (void)setPhotos:(NSArray *)photos
{
    _photos = photos;
    
    NSInteger photosCount = photos.count;
    
    
    // 创建足够数量的图片控件
    // 这里的self.subviews.count不要单独赋值给其他变量
    while (self.subviews.count < photosCount) {
        KDWeiboPhotoView *photoView = [[KDWeiboPhotoView alloc] init];
        [self addSubview:photoView];
    }
    
    // 遍历所有的图片控件，设置图片
    for (int i = 0; i<self.subviews.count; i++) {
        KDWeiboPhotoView *photoView = self.subviews[i];
        
        if (i < photosCount) { // 显示
            photoView.photo = photos[i];
            photoView.hidden = NO;
        } else { // 隐藏
            photoView.hidden = YES;
        }
    }
}
- (void)layoutSubviews
{
    [super layoutSubviews];
    
    // 设置图片的尺寸和位置
    NSInteger photosCount = self.photos.count;
    int maxCol = KDWeiboPhotoMaxCol(photosCount);
    for (int i = 0; i<photosCount; i++) {
        KDWeiboPhotoView *photoView = self.subviews[i];
        
        int col = i % maxCol;
        photoView.x = col * (KDWeiboPhotoWH + KDWeiboPhotoMargin);
        
        int row = i / maxCol;
        photoView.y = row * (KDWeiboPhotoWH + KDWeiboPhotoMargin);
        photoView.width = KDWeiboPhotoWH;
        photoView.height = KDWeiboPhotoWH;
    }
    if (photosCount == 1) {//如果只有一张图片的话，就让它原样显示吧！
        KDWeiboPhotoView *photoView = self.subviews[0];
        CGSize imageSize = [KDWebImageSize downloadImageSizeWithURL:photoView.photo.thumbnail_pic];
        photoView.width = imageSize.width;
        photoView.height = imageSize.height;
    }
}
+ (CGSize)sizeWithCount:(int)count andPhotos:(NSArray *)photos
{
    // 最大列数（一行最多有多少列）
    int maxCols = KDWeiboPhotoMaxCol(count);
    
    int cols = (count >= maxCols)? maxCols : count;
    CGFloat photosW = cols * KDWeiboPhotoWH + (cols - 1) * KDWeiboPhotoMargin;
    
    // 行数
    int rows = (count + maxCols - 1) / maxCols;
    CGFloat photosH = rows * KDWeiboPhotoWH + (rows - 1) * KDWeiboPhotoMargin;
    if (rows == 1 && cols == 1) {//如果只有一张图片
        KDPhoto *photo = photos[0];
        CGSize imageSize = [KDWebImageSize downloadImageSizeWithURL:photo.thumbnail_pic];
        return imageSize;
    }
    return CGSizeMake(photosW, photosH);
}
@end
